<?php
/**
 * 获取IP
 *
 * Author: JennyWang
 * Date: 2018/4/11
 * Time: 21:27
 * Created by PhpStorm.
 */

namespace App\Services\Utils;

use Illuminate\Http\Request;

class IpUtils
{
    protected static $is_proxies_set = false;

    /**
     * IpUtils constructor.
     */
    public function __construct()
    {
        $this->setTrustProxies();
    }

    /**
     * @param array|null $proxies
     * @param null $head_set
     */
    public function setTrustProxies(array $proxies = null, $head_set = null)
    {
        if (! self::$is_proxies_set) {
           Request::setTrustedProxies(
                $proxies ?: config('pkx.proxies'),
                $head_set ?: Request::HEADER_X_FORWARDED_ALL
           );

           self::$is_proxies_set = true;
        }
    }


    /**
     * 返回 IP （需要反向代理 X FORWARD FOR） 请设置 trust proxies env
     *
     * @return null|string
     */
    public function getIp()
    {
        return \request()->getClientIp();
    }
}